/*
 *  ME218_8x8_charset_PGM.h
 *  330AM
 *
 *  Created by Rick Miley on 3/31/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include <ME218_8x8_charset.h>


// Character graphics derived from Chris Eddy data

PROGMEM_XXX prog_uchar_XXX charset[][1+8] = {
	
	{		// null
		6,		// character width

		0x00,			// 00000000
		0x00,			// 00000000
		0x00,			// 00000000
		0x00,			// 00000000
		0x00,			// 00000000
		0x00,			// 00000000
		0x00,			// 00000000
		0x00			// 00000000
	},
	
	{		// !
		2,		// character width

		0x01,
		0x01,
		0x01,
		0x01,
		0x01,
		0x00,
		0x01,
		0x00			// 00000000
	},
	{		// "
		4,		// character width

		0x05,
		0x05,
		0x05,
		0x00,
		0x00,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// #
		6,		// character width

		0x0A,
		0x0A,
		0x1F,
		0x0A,
		0x1F,
		0x0A,
		0x0A,
		0x00			// 00000000
	},
	{		// $
		6,		// character width

		0x04,
		0x0F,
		0x14,
		0x0E,
		0x05,
		0x1E,
		0x04,
		0x00			// 00000000
	},
	{		// %
		6,		// character width

		0x18,
		0x19,
		0x02,
		0x04,
		0x08,
		0x13,
		0x03,
		0x00			// 00000000
	},
	{		// &
		6,		// character width

		0x0C,
		0x12,
		0x14,
		0x08,
		0x15,
		0x12,
		0x0D,
		0x00			// 00000000
	},
	{		// '
		3,		// character width

		0x03,
		0x01,
		0x02,
		0x00,
		0x00,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// (
		5,		// character width

		0x01,
		0x02,
		0x04,
		0x08,
		0x04,
		0x02,
		0x01,
		0x00			// 00000000
	},
	{		// )
		5,		// character width

		0x08,
		0x04,
		0x02,
		0x01,
		0x02,
		0x04,
		0x08,
		0x00			// 00000000
	},
	{		// *
		6,		// character width

		0x00,
		0x04,
		0x15,
		0x0E,
		0x15,
		0x04,
		0x00,
		0x00			// 00000000
	},
	{		// +
		6,		// character width

		0x00,
		0x04,
		0x04,
		0x1F,
		0x04,
		0x04,
		0x00,
		0x00			// 00000000
	},
	{		// , 
		3,		// character width

		0x00,
		0x00,
		0x00,
		0x00,
		0x03,
		0x01,
		0x02,
		0x00			// 00000000

	},
	{		// -
		5,		// character width

		0x00,
		0x00,
		0x00,
		0x0F,
		0x00,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// .
		3,		// character width

		0x00,
		0x00,
		0x00,
		0x00,
		0x00,
		0x03,
		0x03,
		0x00			// 00000000
	},
	
	{		// /
		7,		// character width

		0x00,
		0x01,
		0x02,
		0x04,
		0x08,
		0x10,
		0x20,
		0x00			// 00000000
	},
	{		// 0
		6,		// character width

		0x0E,
		0x11,
		0x13,
		0x15,
		0x19,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// 1
		4,		// character width

		0x02,
		0x06,
		0x02,
		0x02,
		0x02,
		0x02,
		0x07,
		0x00			// 00000000
	},
	{		// 2
		6,		// character width

		0x0E,
		0x11,
		0x01,
		0x02,
		0x04,
		0x08,
		0x1F,
		0x00			// 00000000
	},
	{		// 3
		6,		// character width

		0x1F,
		0x02,
		0x04,
		0x02,
		0x01,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// 4
		6,		// character width

		0x02,
		0x06,
		0x0A,
		0x12,
		0x1F,
		0x02,
		0x02,
		0x00			// 00000000
	},
	{		// 5
		6,		// character width

		0x1F,
		0x10,
		0x1e,
		0x01,
		0x01,
		0x11,
		0x0e,
		0x00			// 00000000
	},
	{		// 6
		6,		// character width

		0x06,
		0x08,
		0x10,
		0x1E,
		0x11,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// 7
		6,		// character width

		0x1F,
		0x01,
		0x02,
		0x04,
		0x04,
		0x04,
		0x04,
		0x00			// 00000000
	},
	{		// 8
		6,		// character width

		0x0E,
		0x11,
		0x11,
		0x0E,
		0x11,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// 9
		6,		// character width

		0x0E,
		0x11,
		0x11,
		0x0F,
		0x01,
		0x02,
		0x0c,
		0x00			// 00000000
	},
	{		// :
		3,		// character width

		0x00,
		0x03,
		0x03,
		0x00,
		0x03,
		0x03,
		0x00,
		0x00			// 00000000
	},
	{		// ; 
		3,		// character width

		0x00,
		0x00,
		0x03,
		0x03,
		0x00,
		0x03,
		0x01,
		0x02
	},
	{		// <
		5,		// character width

		0x01,
		0x02,
		0x04,
		0x08,
		0x04,
		0x02,
		0x01,
		0x00			// 00000000
	},
	{		// =
		5,		// character width

		0x00,
		0x00,
		0x0F,
		0x00,
		0x0F,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// >
		5,		// character width

		0x08,
		0x04,
		0x02,
		0x01,
		0x02,
		0x04,
		0x08,
		0x00			// 00000000
	},
	{		// ?
		6,		// character width

		0x0E,
		0x11,
		0x01,
		0x02,
		0x04,
		0x00,
		0x04,
		0x00			// 00000000
	},
	{		// @
		6,		// character width

		0x0E,
		0x11,
		0x01,
		0x0D,
		0x15,
		0x15,
		0x0E,
		0x00			// 00000000
	},
	{		// A
		6,		// character width
		
		0x0E,		// 00001110
		0x11,		// 00010001
		0x11,		// 00010001
		0x1f,		// 00011111
		0x11,		// 00010001
		0x11,		// 00010001
		0x11,		// 00010001
		0x00		// 00000000

	},
	{		// B
		6,		// character width

		0x1E,
		0x09,
		0x09,
		0x0E,
		0x09,
		0x09,
		0x1E,
		0x00			// 00000000
	},
	
	
	{		// C
		6,		// character width

		0x0E,
		0x11,
		0x10,
		0x10,
		0x10,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// D
		6,		// character width

		0x1E,
		0x09,
		0x09,
		0x09,
		0x09,
		0x09,
		0x1E,
		0x00			// 00000000
	},
	{		// E
		6,		// character width

		0x1F,
		0x10,
		0x10,
		0x1E,
		0x10,
		0x10,
		0x1F,
		0x00			// 00000000
	},
	{		// F
		6,		// character width

		0x1F,
		0x10,
		0x10,
		0x1E,
		0x10,
		0x10,
		0x10,
		0x00			// 00000000
	},
	{		// G
		6,		// character width

		0x0E,
		0x11,
		0x10,
		0x13,
		0x11,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// H
		6,		// character width

		0x11,
		0x11,
		0x11,
		0x1F,
		0x11,
		0x11,
		0x11,
		0x00			// 00000000
	},
	{		// I
		4,		// character width

		0x07,			// 00000111 
		0x02,			// 00000010 
		0x02,			// 00000010 
		0x02,			// 00000010 
		0x02,			// 00000010 
		0x02,			// 00000010 
		0x07,			// 00000111 
		0x00			// 00000000
	},
	{		// J
		6,		// character width

		0x07,			// 00000111 
		0x02,			// 00000010 
		0x02,			// 00000010 
		0x02,			// 00000010
		0x02,			// 00000010 
		0x12,			// 00010010 
		0x0C,			// 00001100
		0x00			// 00000000
	},
	
	{		// K
		6,		// character width

		0x11,			// 00010001
		0x12,			// 00010010
		0x14,			// 00010100
		0x18,			// 00011000
		0x14,			// 00010100
		0x12,			// 00010010		
		0x11,			// 00010001
		0x00			// 00000000
	},
	{		// L
		6,		// character width

		0x10,			// 00010000		
		0x10,			// 00010000		
		0x10,			// 00010000		
		0x10,			// 00010000		
		0x10,			// 00010000		
		0x10,			// 00010000		
		0x1f,			// 00011111		
		0x00			// 00000000		
	
	},
	{		// M
		6,		// character width

		0x11,			// 00010001
		0x1B,			// 00011011
		0x15,			// 00010101
		0x15,			// 00010101
		0x11,			// 00010001
		0x11,			// 00010001
		0x11,			// 00010001
		0x00			// 00000000
	},
	{		// N
		6,		// character width

		0x11,			// 00010001
		0x19,			// 00011001
		0x19,			// 00011001
		0x15,			// 00010101
		0x13,			// 00010011
		0x13,			// 00010011
		0x11,			// 00010001
		0x00			// 00000000
	},
	{		// O
		6,		// character width

		0x0E,			// 00001110
		0x11,			// 00010001
		0x11,			// 00010001
		0x11,			// 00010001
		0x11,			// 00010001
		0x11,			// 00010001
		0x0E,			// 00001110
		0x00			// 00000000
	},
	{		// P
		6,		// character width

		0x1E,			// 00011110
		0x11,			// 00010001
		0x11,			// 00010001
		0x1E,			// 00011110
		0x10,			// 00010000
		0x10,			// 00010000
		0x10,			// 00010000
		0x00			// 00000000
	},
	{		// Q
		6,		// character width

		0x0E,
		0x11,
		0x11,
		0x11,
		0x15,
		0x12,
		0x0D,
		0x00			// 00000000
	},
	{		// R
		6,		// character width

		0x1E,
		0x11,
		0x11,
		0x1E,
		0x14,
		0x12,
		0x11,
		0x00			// 00000000
	},
	{		// S
		6,		// character width

		0x0E,
		0x11,
		0x10,
		0x0E,
		0x01,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// T
		6,		// character width

		0x1F,
		0x04,
		0x04,
		0x04,
		0x04,
		0x04,
		0x04,
		0x00			// 00000000
	},
	{		// U
		6,		// character width

		0x11,
		0x11,
		0x11,
		0x11,
		0x11,
		0x11,
		0x0E,
		0x00			// 00000000
	},
	{		// V
		6,		// character width

		0x11,
		0x11,
		0x11,
		0x11,
		0x11,
		0x0A,
		0x04,
		0x00			// 00000000
	},
	{		// W
		6,		// character width

		0x11,
		0x11,
		0x11,
		0x15,
		0x15,
		0x1B,
		0x11,
		0x00			// 00000000
	},
	{		// X
		6,		// character width

		0x11,
		0x11,
		0x0A,
		0x04,
		0x0A,
		0x11,
		0x11,
		0x00			// 00000000
	},
	{		// Y
		6,		// character width

		0x11,
		0x11,
		0x11,
		0x0A,
		0x04,
		0x04,
		0x04,
		0x00			// 00000000
	},
	{		// Z
		6,		// character width

		0x1F,
		0x01,
		0x02,
		0x04,
		0x08,
		0x10,
		0x1F,
		0x00			// 00000000
	},
	
	{		// [
		4,		// character width

		0x07,
		0x04,
		0x04,
		0x04,
		0x04,
		0x04,
		0x07,
		0x00			// 00000000
	},
	{		// "\"
		7,		// character width

		0x00,
		0x20,
		0x10,
		0x08,
		0x04,
		0x02,
		0x01,
		0x00			// 00000000
	},
	{		// ]
		4,		// character width

		0x07,
		0x01,
		0x01,
		0x01,
		0x01,
		0x01,
		0x07,
		0x00			// 00000000
	},
	{		// ^
		6,		// character width

		0x04,
		0x0A,
		0x11,
		0x00,
		0x00,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// _
		5,		// character width

		0x00,
		0x00,
		0x00,
		0x00,
		0x00,
		0x00,
		0x0F,
		0x00			// 00000000
	},
	
	{		// `
		3,		// character width

		0x04,
		0x02,
		0x01,
		0x00,
		0x00,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// a
		6,		// character width

		0x00,
		0x00,
		0x0E,
		0x01,
		0x0F,
		0x11,
		0x0F,
		0x00			// 00000000
	},
	{		// b
		5,		// character width

		0x00,
		0x08,
		0x08,
		0x0e,
		0x09,
		0x09,
		0x06,
		0x00			// 00000000
	},
	{		// c
		5,		// character width

		0x00,
		0x00,
		0x07,
		0x08,
		0x08,
		0x08,
		0x07,
		0x00			// 00000000
	},
	{		// d
		5,		// character width

		0x00,
		0x01,
		0x01,
		0x07,
		0x09,
		0x09,
		0x07,
		0x00			// 00000000
	},
	{		// e
		5,		// character width

		0x00,
		0x00,
		0x06,
		0x09,
		0x0f,
		0x08,
		0x07,
		0x00			// 00000000
	},
	{		// f
		5,		// character width

		0x00,
		0x03,
		0x04,
		0x0f,
		0x04,
		0x04,
		0x04,
		0x00			// 00000000
	},
	{		// g 
		6,		// character width

		0x00,
		0x00,
		0x0e,
		0x11,
		0x11,
		0x0f,
		0x01,
		0x0E

	},
	{		// h
		5,		// character width

		0x00,
		0x08,
		0x08,
		0x0e,
		0x09,
		0x09,
		0x09,
		0x00			// 00000000
	},
	{		// i
		4,		// character width

		0x00,
		0x02,
		0x00,
		0x06,
		0x02,
		0x02,
		0x03,
		0x00			// 00000000
	},
	{		// j 
		3,		// character width

		0x00,
		0x01,
		0x00,
		0x03,
		0x01,
		0x01,
		0x01,
		0x03
	},
	{		// k
		5,		// character width

		0x00,
		0x08,
		0x08,
		0x0A,
		0x0C,
		0x0A,
		0x09,
		0x00			// 00000000
	},
	{		// l
		4,		// character width

		0x00,
		0x06,
		0x02,
		0x02,
		0x02,
		0x02,
		0x03,
		0x00			// 00000000
	},
	{		// m
		6,		// character width

		0x00,
		0x00,
		0x1A,
		0x15,
		0x15,
		0x15,
		0x15,
		0x00			// 00000000
	},
	{		// n
		5,		// character width

		0x00,
		0x00,
		0x0e,
		0x09,
		0x09,
		0x09,
		0x09,
		0x00			// 00000000
	},
	{		// o
		5,		// character width

		0x00,
		0x00,
		0x06,
		0x09,
		0x09,
		0x09,
		0x06,
		0x00			// 00000000
	},
	{		// p 
		5,		// character width

		0x00,
		0x00,
		0x0e,
		0x09,
		0x09,
		0x0e,
		0x08,
		0x08
	},
	{		// q 
		5,		// character width

		0x00,
		0x00,
		0x07,
		0x09,
		0x09,
		0x07,
		0x01,
		0x01
	},
	{		// r
		5,		// character width

		0x00,
		0x00,
		0x0e,
		0x09,
		0x08,
		0x08,
		0x08,
		0x00			// 00000000
	},
	{		// s
		6,		// character width

		0x00,
		0x00,
		0x0E,
		0x10,
		0x0E,
		0x01,
		0x0E,
		0x00			// 00000000
	},
	{		// t
		4,		// character width

		0x00,
		0x02,
		0x02,
		0x07,
		0x02,
		0x02,
		0x01,
		0x00			// 00000000
	},
	{		// u
		6,		// character width

		0x00,
		0x00,
		0x12,
		0x12,
		0x12,
		0x12,
		0x0D,
		0x00			// 00000000
	},
	{		// v
		6,		// character width

		0x00,
		0x00,
		0x11,
		0x11,
		0x11,
		0x0A,
		0x04,
		0x00			// 00000000
	},
	{		// w
		6,		// character width

		0x00,
		0x00,
		0x11,
		0x11,
		0x15,
		0x15,
		0x0A,
		0x00			// 00000000
	},
	{		// x
		6,		// character width

		0x00,
		0x00,
		0x11,
		0x0A,
		0x04,
		0x0A,
		0x11,
		0x00			// 00000000
	},
	{		// y 
		5,		// character width

		0x00,
		0x00,
		0x09,
		0x09,
		0x09,
		0x07,
		0x01,
		0x06
	},
	{		// z
		6,		// character width

		0x00,
		0x00,
		0x1F,
		0x02,
		0x04,
		0x08,
		0x1F,
		0x00			// 00000000
	},
	{		// {
		4,		// character width

		0x01,
		0x02,
		0x02,
		0x04,
		0x02,
		0x02,
		0x01,
		0x00			// 00000000
	},
	{		// |
		2,		// character width

		0x01,
		0x01,
		0x01,
		0x01,
		0x01,
		0x01,
		0x01,
		0x00			// 00000000
	},
	{		// }
		4,		// character width

		0x04,
		0x02,
		0x02,
		0x01,
		0x02,
		0x02,
		0x04,
		0x00			// 00000000
	},
	{		// ~
		6,		// character width

		0x08,
		0x15,
		0x02,
		0x00,
		0x00,
		0x00,
		0x00,
		0x00			// 00000000
	},
	{		// DEL
		6,		// character width

		0xff,			// 11111111
		0xff,			// 11111111
		0xff,			// 11111111
		0xff,			// 11111111
		0xff,			// 11111111
		0xff,			// 11111111
		0xff,			// 11111111
		0xff			// 11111111
	}
};



// ===========================================================
// ========================= LED Marquee =====================
// ===========================================================


// ======== milli_delay =============

void milli_delay(long delta) {
	long temp;
	
	// durations long than a minute are ignored
	if(delta>0 && delta<60000) {
		delta=delta*31;				// convert from MIDI speed of 31,250
									// to milliseconds
	
		temp=_quick_ticks+delta;	// _quick_ticks is updated in main interrupt
		while (temp>_quick_ticks) { }		// spin, round robin would be good here
	}
};


// ======== shiftScreenLeft =============

void shiftScreenLeft() {
	int i,j;
	for(j=0;j<8;j++) {
		for(i=0;i<7;i++) {
			
			// == it's just a jump to the left ==
			OffScreenGameSlate[j][i]=OffScreenGameSlate[j][i+1];		 
		}
		OffScreenGameSlate[j][7]=0;
	}
}


// =========== write_ASCII_LED_char ============

void write_ASCII_LED_char (unsigned char ASCII_char, int speed ) {
	int	i, j;
	unsigned char CLUT_value,char_width,mask_value;			
	prog_uchar_XXX ASCII_data_mask[8];
	
	ASCII_char -=0x20;		// the first 32 characters are null
	ASCII_char &= 0x7f;		// graphics characters are above 0x80
							// they are not yet included
	
	CLUT_value=14;		// 
	
	// ===== read data from FLASH =====

	char_width=pgm_read_byte_near(&charset[ASCII_char][0]);

	for(i=1;i<9;i++) {
		// these characters act like "bit sprites"
		ASCII_data_mask[i-1]=pgm_read_byte_near(&charset[ASCII_char][i]);
	}
	
	// ================ Kerning ====================
	switch (char_width) {
		case 8:
			mask_value = 0x80;
			break;
		case 7:
			mask_value = 0x40;
			break;
		case 6:
			mask_value = 0x20;
			break;
		case 5:
			mask_value = 0x10;
			break;
		case 4:
			mask_value = 0x08;
			break;
		case 3:
			mask_value = 0x04;
			break;
		case 2:
			mask_value = 0x02;
			break;
	}
		
	
	for (i=0;i<char_width;i++) {
		shiftScreenLeft();		// OffScreenGameSlate array is used to
								// store the working Marquee values
		
		for (j=7;j>=0;j--) {
			// fill in the rightmost column
			if((ASCII_data_mask[7-j] & mask_value)!=0)
				OffScreenGameSlate[j][7]=CLUT_value;		// turn on pixel
			else
				OffScreenGameSlate[j][7]=0;		// turn off pixel
			}
		
			milli_delay(speed);
		
		mask_value=mask_value>>1;
		
	}
}

// =========== draw_ASCII_LED_char ===========
// by using a speed of zero, the character 
// will not move


void draw_ASCII_LED_char (unsigned char ASCII_char) {
	
	// set the speed to zero
	write_ASCII_LED_char(ASCII_char,0);		// draw character without scrolling
	shiftScreenLeft();						// the character is up against the
											// right. Move in one pixel
}

// ================= Flash Cursor =================

void flash_ASCII_LED_char (unsigned char the_ASCII_char, int how_many) {
	int count;
	
	if (how_many<0) {
			// ========= forever
		while (1) {
			write_ASCII_LED_char(the_ASCII_char,0);	// draw character without scrolling
			milli_delay(1000);
			write_ASCII_LED_char(0,0);		// draw character without scrolling
			milli_delay(1000);
		}
	}
	else {
		for (count=0;count<how_many;count++) {
			write_ASCII_LED_char(the_ASCII_char,0);		// draw character without scrolling
			milli_delay(1000);
			write_ASCII_LED_char(0,0);				// draw character without scrolling
			milli_delay(1000);
		}
	}
}
		
		
		
			 
			 



// =========== scroll_ASCII_LED_string ===========

void scroll_ASCII_LED_string (const char *the_ASCII_string, int speed) {
	int i, string_length;
	

	write_ASCII_LED_char(0x20,speed);					// scroll in a NULL character

	string_length = strlen(the_ASCII_string);	// how long is the string
	
	// limit strings to 100 characters
	if (string_length>0 && string_length<100) {
		for (i=0;i<string_length;i++) {
			write_ASCII_LED_char(*the_ASCII_string,speed);	// scroll character
			the_ASCII_string++;
		}
	}
}

// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========
// ============= sample test LED Marquee ===========

void scroll_LED_TEST (void) {
	int i,speed;
	
//	flash_ASCII_LED_char('C',-1);
	
	speed=150;			
	// == draw one character at a time ===
//	draw_ASCII_LED_char('A');
	//milli_delay(4000);	// four seconds

//	draw_ASCII_LED_char('I');
//	milli_delay(4000);	// four seconds
//	

	while (1) {
		scroll_ASCII_LED_string("Apple",speed);
		scroll_ASCII_LED_string("Banana",speed);
		scroll_ASCII_LED_string("Coconut",speed);
		
		
		// write out all printable characters
		for(i='!';i<'~';i++)
//		for(i='a';i<'z';i++)
			write_ASCII_LED_char(i,speed);		// scroll characters

	}
};
		
					  


